package com.jsmy.marketing.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jsmy.marketing.R;
import com.jsmy.marketing.adapter.NewsAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.bean.BaseBean;
import com.jsmy.marketing.bean.NewsBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.manager.UserManager;
import com.jsmy.marketing.utils.ToastUtil;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.aigestudio.datepicker.cons.DPMode;
import cn.aigestudio.datepicker.views.DatePicker;

/**
 * 添加计划
 */

public class AddPlantActivity extends BaseActivity {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_add_plan_student)
    TextView mTvName;
    @BindView(R.id.edt_choose_time)
    EditText mEdtTime;
    @BindView(R.id.edt_choose_resource)
    EditText mEdtResource;
    @BindView(R.id.edt_add_plan_text)
    EditText mEdtText;
    private NewsBean newsBean;


    @Override
    protected int resId() {
        return R.layout.activity_add_plant;
    }

    @Override
    protected void initData() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTvName.setText(getPrevExtras().getString("name"));
    }

    @OnClick({R.id.edt_choose_time, R.id.edt_choose_resource, R.id.btn_submit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edt_choose_time:
                final Dialog dialog=new Dialog(this);

                DatePicker picker = new DatePicker(this);
                picker.setDate(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH) + 1);
                picker.setMode(DPMode.SINGLE);
                picker.setTodayDisplay(true);
                picker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
                    @Override
                    public void onDatePicked(String date) {
                        mEdtTime.setText(date);
                        dialog.dismiss();
                    }
                });
                dialog.setContentView(picker);
                dialog.show();
                break;
            case R.id.edt_choose_resource:
                requestNews();
                break;
            case R.id.btn_submit:

                savegtplaninfo(getPrevExtras().getString("kid"), mEdtText.getText().toString().trim(), mEdtTime.getText().toString().trim(), url, zilid);
                break;
            default:
                break;
        }
    }
    String url = "";
    String zilid = "";
    public void savegtplaninfo(String kid, String nr, String sj, String url, String zlid) {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", UserManager.getInstance().getId());
        map.put("kid", kid);
        map.put("nr", nr);
        map.put("sj", sj);
        map.put("gtlx", "");
        map.put("url", url);
        map.put("zlid", zlid);
        map.put("isAND", "Y");
        mPresenter.loadData(RetrofitManager.getInstance().savegtplaninfo(map), Link.APP_SAVE_GTPLAN_INFO);
    }

    public void requestNews(){
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        mPresenter.loadData(RetrofitManager.getInstance().requestNews(map), Link.APP_ZILIAO_choose);
    }
    @Override
    public void refreshData(Object object, String api_id) {
        switch (api_id) {
            case Link.APP_ZILIAO_choose:
                newsBean = (NewsBean) object;
                NewsAdapter fzAdapter = new NewsAdapter(this, newsBean.getData().getList());
                TextView textView = new TextView(this);
                textView.setGravity(Gravity.CENTER);
                textView.setText("选择资源");
                textView.setTextColor(Color.BLACK);
                DialogPlus dialog = DialogPlus.newDialog(this)
                         .setHeader(textView)
                        .setAdapter(fzAdapter)
                        .setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                dialog.dismiss();
                                NewsBean.DataBean.ListBean listBean = (NewsBean.DataBean.ListBean) item;
                                if (!TextUtils.isEmpty(listBean.getTitle())){
                                    mEdtResource.setText(listBean.getTitle());
                                    url=listBean.getTitle();
                                    zilid=listBean.getId()+"";
                                }
                            }

                        })
                        .setExpanded(true)
                        .create();
                dialog.show();
                break;
            case Link.APP_SAVE_GTPLAN_INFO:
                BaseBean baseBean= (BaseBean) object;
                if (baseBean.getCode().equals("Y")){
                    ToastUtil.showShort(this,"添加成功");
                    setResult(100);
                    finish();
                }else {
                    ToastUtil.showShort(this,baseBean.getMsg());
                }
                break;
            default:
                break;
        }
    }
}
